package br.usp.poli.util.db;

import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Session;

import br.usp.poli.entity.DataSetMetadata;
import br.usp.poli.entity.Experiment;
import br.usp.poli.entity.Instrument;
import br.usp.poli.entity.Keyword;
import br.usp.poli.entity.Laboratory;
import br.usp.poli.entity.Person;
import br.usp.poli.entity.Variable;
import eml.ecoinformatics_org.attribute_2_0.UnitDictionary;

public class PopulaBanco {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Laboratory ib = createLab();
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		persistLab(ib, session);
		
		Experiment e = persistExperiment(session);
		
		session.persist(e);
		
		session.getTransaction().commit();
		session.close();
	}

	@SuppressWarnings("unchecked")
	private static Experiment persistExperiment(Session session) {
		Experiment e = new Experiment();
		e.setStatus(Experiment.Status.SUBMITED);
		DataSetMetadata dataSetMetadata = e.getDataSetMetadata();
		
		dataSetMetadata.setTitle("Experimento envolvendo WebLabs de Abelhas");
		
		Set<Person> creators = new HashSet<Person>();
		
		Person creator = new Person();
		creator.setSurName("Bessa");
		creator.setGivenName("Leonardo");
		creator.setSalutation("Mr.");
		creators.add(creator);
		session.persist(creator);
		
		Person creator2 = new Person();
		creator2.setSurName("Giannini");
		creator2.setGivenName("Cris");
		creator2.setSalutation("Mrs.");
		creators.add(creator2);
		session.persist(creator2);
		
		dataSetMetadata.setCreator(creators);
		
		List<Variable> variables = session.createCriteria(Variable.class).list();
		e.setVariables(variables );
		
		dataSetMetadata.setEndDate(new GregorianCalendar(2008,02-1,23,23,0,0));
		dataSetMetadata.setStartDate(new GregorianCalendar(2007,10-1,22,23,0,0));
		
		Set<Keyword> keywords = new HashSet<Keyword>();
		Keyword keyword1 = new Keyword("Melipona scutellaris");
		keywords.add(keyword1);
		session.persist(keyword1);
		return e;
	}

	private static void persistLab(Laboratory ib, Session session) {
		session.save(ib);
		for (Instrument i : ib.getInstruments()){
			session.save(i);
			for (Variable v : i.getVariables()){
				session.save(v);
			}
		}
	}

	private static Laboratory createLab() {
		Laboratory lab = new Laboratory();
		lab.setId(1L);
		lab.setName("Laboratorio de Abelhas - IB");
		lab.add(createHobo("1056784"));
		lab.add(createHobo("1056806"));
		lab.add(createHobo("1056813"));
		lab.add(createHobo("1056808"));
		lab.add(createWheatherStation("0000000"));
		lab.add(createXarope("colmeia1"));
		lab.add(createXarope("colmeia9"));
		lab.add(createXarope("colmeia3"));
		return lab;
	}
	
	private static Instrument createWheatherStation(String serial) {
		Instrument wheatherStation = new Instrument("WheatherStation", serial);
		wheatherStation.add(createVariable(wheatherStation, "Temperature", UnitDictionary.CELSIUS));
		wheatherStation.add(createVariable(wheatherStation, "Humidity", UnitDictionary.DIMENSIONLESS));
		return wheatherStation;
	}
	
	private static Instrument createXarope(String serial) {
		Instrument xarope = new Instrument("XAROPE", serial);
		for (int i=1;i<10;i++){
			xarope.add(createVariable(xarope, i+"0%(ml consumidos)", UnitDictionary.DIMENSIONLESS));
		}
		return xarope;
	}

	private static Instrument createHobo(String serial) {
		Instrument hobo = new Instrument("HOBO", serial);
		hobo.add(createVariable(hobo, "Temperature", UnitDictionary.CELSIUS));
		hobo.add(createVariable(hobo, "Humidity", UnitDictionary.DIMENSIONLESS));
		return hobo;
	}

	private static Variable createVariable(Instrument instrument, String name, UnitDictionary unit) {
		Variable v = new Variable(instrument);
		v.setName(name);
		v.setUnit(unit.value());
		
		v.setMissingCodeValue("---");
		v.setMissingCodeValueExplanation("No value.");
		
		v.setPrecisonValue(0.01f);
		v.setNumberType("real");
		
//	    AttributeType attributeType = new AttributeType();
//		
//		
//		attributeType.setAttributeName(name);
//		MissingValueCode missingValueCode = new MissingValueCode();
//		missingValueCode.setCode("null");
//		missingValueCode.setCodeExplanation("No value.");
//		attributeType.getMissingValueCode().add(missingValueCode );
//		MeasurementScale measurementScale = new AttributeType.MeasurementScale();
//		attributeType.setMeasurementScale(measurementScale);
//		Ratio ratio = new MeasurementScale.Ratio();
//		measurementScale.setRatio(ratio);
//		PrecisionType precision = new PrecisionType();
//		precision.setValue(0.01f);
//		ratio.setPrecision(precision);
//		UnitType unitType = new UnitType();
//		unitType.setStandardUnit(unit);
//		ratio.setUnit(unitType);
//		NumericDomainType numericDomainType = new NumericDomainType();
//		numericDomainType.setNumberType(NumberType.REAL);
//		ratio.setNumericDomain(numericDomainType);
//		
//		v.setAttributeType(attributeType);
		
		
		return v;
	}
	
	

}
